﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace QLua.Method;

class EventHandlerContainer : IDisposable
{
	private readonly Dictionary<Delegate, RegisterEventHandler> _dict = new Dictionary<Delegate, RegisterEventHandler>();

	public void Add(Delegate handler, RegisterEventHandler eventInfo)
	{
		_dict.Add(handler, eventInfo);
	}

	public void Remove(Delegate handler)
	{
		bool found = _dict.Remove(handler);
		Debug.Assert(found);
	}

	/// <summary>
	/// Remove any still registered handlers
	/// </summary>
	public void Dispose()
	{
		foreach (KeyValuePair<Delegate, RegisterEventHandler> pair in _dict)
			pair.Value.RemovePending(pair.Key);

		_dict.Clear();
	}
}
